// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.ui.query;

import java.util.Collection;
import java.util.List;

import org.dengues.commons.IDenguesSharedImage;
import org.dengues.commons.utils.ImageUtil;
import org.dengues.model.database.DatabaseDiagram;
import org.dengues.ui.i18n.Messages;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 2007-12-16 qiang.zhang $
 * 
 */
public class FilterValueDialog extends Dialog {

    public static final String QUERY_PREFIX = "@"; //$NON-NLS-1$

    private String value;

    private final Collection<DatabaseDiagram> zQueries;

    private Text zValueText;

    private String wholeValue;

    private final Object zObject;

    /**
     * Qiang.Zhang.Adolf@gmail.com FilterValueDialog constructor comment.
     * 
     * @param parentShell
     * @param warehouseQueries
     * @param object
     */
    public FilterValueDialog(Shell parentShell, Collection<DatabaseDiagram> warehouseQueries, Object object) {
        super(parentShell);
        setShellStyle(getShellStyle() | SWT.RESIZE | SWT.MAX);
        this.zQueries = warehouseQueries;
        this.zObject = object;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
     */
    @Override
    protected void configureShell(Shell newShell) {
        super.configureShell(newShell);
        newShell.setText(Messages.getString("FilterValueDialog.filterTitle")); //$NON-NLS-1$
        newShell.setSize(400, 500);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
     */
    @Override
    protected Control createDialogArea(Composite parent) {
        Composite areaContainer = (Composite) super.createDialogArea(parent);
        Composite valueComp = new Composite(areaContainer, SWT.NONE);
        GridLayout gridLayout = new GridLayout(2, false);
        valueComp.setLayout(gridLayout);
        GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
        valueComp.setLayoutData(gridData);
        Label label = new Label(valueComp, SWT.NONE);
        label.setText(Messages.getString("FilterValueDialog.value")); //$NON-NLS-1$
        zValueText = new Text(valueComp, SWT.BORDER);
        if (zObject != null) {
            zValueText.setText(zObject.toString());
        }
        gridData = new GridData(GridData.FILL_HORIZONTAL);
        zValueText.setLayoutData(gridData);
        SashForm composite = new SashForm(areaContainer, SWT.VERTICAL);
        gridLayout = new GridLayout();
        composite.setLayout(gridLayout);
        gridData = new GridData(GridData.FILL_BOTH);
        composite.setLayoutData(gridData);

        final TreeViewer treeViewer = new TreeViewer(composite, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
        gridData = new GridData(GridData.FILL_BOTH);
        treeViewer.getTree().setLayoutData(gridData);
        TreeViewerProvider provider = new TreeViewerProvider();
        treeViewer.setContentProvider(provider);
        treeViewer.setLabelProvider(provider);
        treeViewer.setInput(zQueries);
        treeViewer.expandAll();
        final Text text = new Text(composite, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI);
        text.setEditable(false);
        composite.setWeights(new int[] { 1, 1 });
        treeViewer.addSelectionChangedListener(new ISelectionChangedListener() {

            /*
             * (non-Javadoc)
             * 
             * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
             */
            public void selectionChanged(SelectionChangedEvent event) {
                Object elem = ((IStructuredSelection) event.getSelection()).getFirstElement();
                if (elem instanceof DatabaseDiagram) {
                    text.setText(((DatabaseDiagram) elem).getWholeSql());
                    wholeValue = text.getText();
                    zValueText.setText(QUERY_PREFIX + ((DatabaseDiagram) elem).getName());
                }
            }
        });
        return areaContainer;
    }

    public String getValue() {
        return this.value;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.dialogs.Dialog#okPressed()
     */
    @Override
    protected void okPressed() {
        value = zValueText.getText();
        super.okPressed();
    }

    class TreeViewerProvider extends LabelProvider implements ITreeContentProvider {

        /*
         * (non-Javadoc)
         * 
         * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
         */
        public Object[] getChildren(Object parentElement) {
            if (parentElement instanceof List) {
                return ((List) parentElement).toArray(new Object[0]);
            }
            return new Object[0];
        }

        /*
         * (non-Javadoc)
         * 
         * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
         */
        public Object getParent(Object element) {
            return null;
        }

        /*
         * (non-Javadoc)
         * 
         * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
         */
        public boolean hasChildren(Object element) {
            return false;
        }

        /*
         * (non-Javadoc)
         * 
         * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
         */
        public Object[] getElements(Object inputElement) {
            return getChildren(inputElement);
        }

        /*
         * (non-Javadoc)
         * 
         * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer,
         * java.lang.Object, java.lang.Object)
         */
        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {

        }

        /*
         * (non-Javadoc)
         * 
         * @see org.eclipse.jface.viewers.LabelProvider#getImage(java.lang.Object)
         */
        @Override
        public Image getImage(Object element) {
            if (element instanceof DatabaseDiagram) {
                return ImageUtil.getImage(IDenguesSharedImage.VIEW_QUERY);
            }
            return super.getImage(element);
        }

        /*
         * (non-Javadoc)
         * 
         * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
         */
        @Override
        public String getText(Object element) {
            if (element instanceof DatabaseDiagram) {
                return ((DatabaseDiagram) element).getName();
            }
            return super.getText(element);
        }

    }

    /**
     * Getter for wholeValue.
     * 
     * @return the wholeValue
     */
    public String getWholeValue() {
        return this.wholeValue;
    }
}
